<template>
    <div>
      <cfg-table
        ref="table"
        :columns="columns"
        :data="data"
        :loading="loading"
        :config="config"
        :pageSizeOpts="pageSizeOpts"
        :pageNavigation="pageNavigation"
        :queryFormItem="queryFormItem"
        :collapseFormItem="collapseFormItem"
        :additionalAction="additionalAction"
        @loadPage="loadPage"
        @handleCreate="handleCreate"
        @handleDetail="handleDetail"
        @handleEdit="handleEdit"
        @handleDelete="handleDelete"
        @handleDeleteMultiple="handleDeleteMultiple"
        @handleSearch="handleSearch"
        @handleReset="handleReset"
      ></cfg-table>
      <read-modal
        v-bind="readPanel"
        :modelType="modelType"
        :columns="columns"
        :rules="rules"
      ></read-modal>
    </div>
</template>

  <script>
    import readModal from '@/pages/components/readModal.vue';
    import { typeClassName } from '@/pages/config/classNameMap';
    import mixin from '@/pages/components/mixins/mixin'
    export default {
        components: { readModal },
        name: 'energyStandardCoal',
        mixins: [mixin],
        data () {
            return {
                classObject: null, // 单个实例
                collectionObject: null, // 所有实例
                className: typeClassName['energyStandardCoal'].className,
                type: typeClassName['energyStandardCoal'].type,
                modelType: typeClassName['energyStandardCoal'].modelType,
                typeOptions: [], // 能源类型下拉框
                typeClassObject: null, // 能源类型实例
                readPanel: {
                    width: 1024,
                    zIndex: 100,
                    titleStyle: {},
                    drawer: {
                        show: false
                    },
                    cols: 2,
                    data: {}
                },
                rules: {
                    sf_energyCode: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ],
                    sf_energyName: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
                loading: false,
                config: {
                    search: true,
                    addButton: false,
                    batchDeleteButton: true,
                    tableSizeIcon: true,
                    fullscreenIcon: true,
                    refreshIcon: false,
                    columnSettingIcon: true,
                    action: false,
                    additionalAction: false
                },
                pageSizeOpts: [10, 30, 50, 100],
                pageNavigation: { total: 0, pageSize: 10, pageNumber: 1 },
                sourceOptions: [{label: '外采', value: 'out'}, {label: '自产', value: 'product'}],
            };
        },
        props: {
          energyCode: {
            default: () => ''
          },
          energyName: {
            default: () => ''
          },
        },
        computed: {
            // 表格中列字段
            columns() {
              return [
                {
                  key: 'sf_energyCode',
                  title: {
                    'zh-CN': '能源编码',
                    'en-US': 'sf_energyCode'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'sf_energyName',
                  title: {
                    'zh-CN': '能源名称',
                    'en-US': 'sf_energyName'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'simpleCode',
                  title: {
                    'zh-CN': '能源简码',
                    'en-US': 'simpleCode'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  show: true
                },
                {
                  key: 'source',
                  title: {
                    'zh-CN': '能源来源',
                    'en-US': 'source'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true,
                  render: (h, params) => {
                    return h('div', {}, this.getOptionValue(this.sourceOptions, params.row.source))
                  },
                  config: { type: 'select', options: this.sourceOptions, disabled: true }
                },
                {
                  key: 'type',
                  title: {
                    'zh-CN': '能源类型',
                    'en-US': 'type'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true,
                  render: (h, params) => {
                    return h('div', {}, this.getOptionValue(this.typeOptions, params.row.type))
                  },
                  config: { type: 'select', options: this.typeOptions, disabled: true }
                },
                {
                  key: 'measuringUnit',
                  title: {
                    'zh-CN': '计量单位',
                    'en-US': 'measuringUnit'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'point',
                  title: {
                    'zh-CN': '数采点位',
                    'en-US': 'point'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'coefficient',
                  title: {
                    'zh-CN': '标准煤系数',
                    'en-US': 'coefficient'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'remark',
                  title: {
                    'zh-CN': '备注',
                    'en-US': 'remark'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  show: true
                },
                {
                  key: 'createdBy',
                  title: {
                    'zh-CN': '创建人账号',
                    'en-US': 'created by'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'createTime',
                  title: {
                    'zh-CN': '创建时间',
                    'en-US': 'create time'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'updatedBy',
                  title: {
                    'zh-CN': '修改人账号',
                    'en-US': 'updated by'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                },
                {
                  key: 'updateTime',
                  title: {
                    'zh-CN': '修改时间',
                    'en-US': 'update time'
                  }[this.G_Locale],
                  minWidth: 200,
                  align: 'center',
                  show: true
                }
              ];
            },
            // form中列字段
            formColumns() {
              return [
                {
                  key: 'sf_energyCode',
                  title: {
                    'zh-CN': '能源编码',
                    'en-US': 'sf_energyCode'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'text' }
                },
                {
                  key: 'sf_energyName',
                  title: {
                    'zh-CN': '能源名称',
                    'en-US': 'sf_energyName'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'text' }
                },
                {
                  key: 'simpleCode',
                  title: {
                    'zh-CN': '能源简码',
                    'en-US': 'simpleCode'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'text' }
                },
                {
                  key: 'source',
                  title: {
                    'zh-CN': '能源来源',
                    'en-US': 'source'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'select', options: this.sourceOptions }
                },
                {
                  key: 'type',
                  title: {
                    'zh-CN': '能源类型',
                    'en-US': 'type'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  show: true,
                  config: { type: 'select', options: this.typeOptions }
                },
                {
                  key: 'measuringUnit',
                  title: {
                    'zh-CN': '计量单位',
                    'en-US': 'measuringUnit'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'text' }
                },
                {
                  key: 'point',
                  title: {
                    'zh-CN': '数采点位',
                    'en-US': 'point'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'text' }
                },
                {
                  key: 'coefficient',
                  title: {
                    'zh-CN': '标准煤系数',
                    'en-US': 'coefficient'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'number' }
                },
                {
                  key: 'remark',
                  title: {
                    'zh-CN': '备注',
                    'en-US': 'remark'
                  }[this.G_Locale],
                  minWidth: 100,
                  align: 'center',
                  config: { type: 'textarea' }
                }
              ];
            },
            queryFormItem() {
              return [
                {
                  key: 'sf_energyCode',
                  title: {
                    'zh-CN': '能源编码',
                    'en-US': 'sf_energyCode'
                  }[this.G_Locale],
                  config: {
                    type: 'text'
                  }
                },
                {
                  key: 'sf_energyName',
                  title: {
                    'zh-CN': '能源名称',
                    'en-US': 'sf_energyName'
                  }[this.G_Locale],
                  config: {
                    type: 'text'
                  }
                }
              ];
            },
            collapseFormItem() {
              return [
                {
                  key: 'sf_energyCode',
                  title: {
                    'zh-CN': '能源编码',
                    'en-US': 'sf_energyCode'
                  }[this.G_Locale],
                  config: {
                    type: 'text'
                  }
                },
                {
                  key: 'sf_energyName',
                  title: {
                    'zh-CN': '能源名称',
                    'en-US': 'sf_energyName'
                  }[this.G_Locale],
                  config: {
                    type: 'text'
                  }
                }
              ];
            },
            lang() {
              return {
                'en-US': {
                  cancel: 'cancel',
                  confirmDelete: 'confirm delete?'
                },
                'zh-CN': {
                  cancel: '取消',
                  confirmDelete: '确认删除？'
                }
              }[this.G_Locale]
            },
            additionalAction () {
                return [];
            },
            data () {
                if (!!this.collectionObject) {
                    return this.collectionObject.records.map(d => {
                        return {
                            ...d,
                            ...d.values
                        }
                    })
                } else {
                    return [];
                }
            }
        },
        beforeMount () {
            this.collectionObject = this.$modelObject.create(`${this.modelType}Collection`);
            this.classObject = this.$modelObject.create(this.modelType);
            this.typeClassObject = this.$modelObject.create(`${typeClassName['energyType'].modelType}`);
            this.setOptionsByHier('energyType', this.typeClassObject, 'typeOptions', {
                label: 'sf_name', value: 'sf_code'
            })
        },
        watch: {
          energyCode:{
            handler(val) {
              if(val) {
                this.$nextTick(() => {
                  this.loadPage(this.pageNavigation, {sf_energyCode: this.energyCode, sf_energyName: this.energyName});
                })
              }
            },
            immediate: true
          }
        },
        mounted () {},
        methods: {
        }
    };
  </script>
  <style scoped lang="scss"></style>
